export const parallelTask = (
  tasks: Array<any>,
  parallelCount = navigator.hardwareConcurrency || 4,
) => {
  return new Promise((resolve, reject) => {
    if (tasks.length === 0) resolve(0);
    let nextIndex = 0,
      finishCount = 0;
    function _run() {
      const task = tasks[nextIndex];
      task().then(() => {
        finishCount++;
        nextIndex++;
        if (nextIndex < tasks.length) {
          _run();
        } else if (finishCount === tasks.length) {
          resolve(1);
        }
      });
    }
    for (let i = 0; i < parallelCount && i < tasks.length; i++) {
      _run();
    }
  });
};
